limsKnowledgelibrary.editPage = {

    getActivedGridParam: function () {
        var _this = this;
        return {
            type: 'grid',
            id: 'lims-knowledgelibrary-edit-list-grid',
            url: limsKnowledgelibrary.baseUrl + '/queries',
            service: 'limsKnowledgelibraryServiceImpl',
            dbTable: 'T_LIMS_KNOWLEDGELIBRARY',
            columns: limsKnowledgelibrary.getEditListColumns('lims-knowledgelibrary-edit-list-grid'),
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0);
                }
            },
            onRowActive: function (index, row) {
            },
            deleteFormatter: function (row) {
                return row.fileName
            },
            page: true,
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BUTTON.INSERT',
                icon: 'add',
                onClick: function () {
                    var model = Gikam.create('modal', {
                        title: 'GIKAM.BUTTON.INSERT',
                        height: '250',
                        width: '400',
                    });
                    Gikam.create('layout', {
                        renderTo: model.window.$dom,
                        center: {
                            items: [{
                                type: 'form',
                                id: 'insert-knowledgelibrary-form',
                                titleWidth: 80,
                                fields: limsKnowledgelibrary.getBaseInfoFormFields(),
                                columns: 1,
                                onLoadSuccess: function () {
                                }
                            }, {
                                type: 'btnToolbar',
                                items: [{
                                    type: 'button',
                                    text: 'GIKAM.BUTTON.CONFIRM',
                                    class: 'blue',
                                    icon: 'save',
                                    color: 'white',
                                    onClick: function () {
                                        var form = Gikam.getComp('insert-knowledgelibrary-form');
                                        if (form.validate()) {
                                            var data = Gikam.getComp('insert-knowledgelibrary-form').getData()
                                            var url = limsKnowledgelibrary.baseUrl;
                                            Gikam.postText(url, Gikam.getJsonWrapper(null, [null, [data]])).done(function () {
                                                Gikam.getComp('lims-knowledgelibrary-edit-list-grid').refresh();
                                            })
                                            Gikam.getLastModal().close(data);
                                        }
                                    }
                                }, {
                                    type: 'button',
                                    text: 'GIKAM.BUTTON.CANCEL',
                                    icon: 'back',
                                    onClick: function () {
                                        Gikam.getLastModal().close();
                                    }
                                }]
                            }]
                        }
                    })
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.DELETE',
                icon: 'remove-row',
                onClick: function () {
                    Gikam.getComp('lims-knowledgelibrary-edit-list-grid').deleteRows(limsKnowledgelibrary.baseUrl);
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.DEACTIVE',
                icon: 'disable',
                onClick: function () {
                    var grid = Gikam.getComp('lims-knowledgelibrary-edit-list-grid');
                    var selections = grid.getSelections()
                    if (Gikam.isEmpty(selections)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                        return
                    }
                    if (selections.length > 0) {
                        Gikam.confirm('CORE.MODULE.ITEM.MESSAGES.TIP.CONFIRM', 'MODULE.COREDATA.METHOD.TIP.CONTINUE_DISUSE', function () {
                            Gikam.postText(limsKnowledgelibrary.baseUrl + '/set-activestatus', Gikam.getJsonWrapper({activeStatus: '0'}, [null, selections])).done(function (data) {
                                grid.refresh()
                            })
                        })
                    }
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.ACTIVE',
                icon: 'enable',
                onClick: function () {
                    var grid = Gikam.getComp('lims-knowledgelibrary-edit-list-grid');
                    var selections = grid.getSelections();
                    if (Gikam.isEmpty(selections)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                        return
                    }
                    if (selections.length > 0) {
                        Gikam.confirm('CORE.MODULE.ITEM.MESSAGES.TIP.CONFIRM', 'MODULE.COREDATA.METHOD.TIP.CONTINUE_ENABLE', function () {
                            Gikam.postText(limsKnowledgelibrary.baseUrl + '/set-activestatus', Gikam.getJsonWrapper({activeStatus: '1'}, [null, selections])).done(function (data) {
                                grid.refresh()
                            });
                        })
                    }
                }
            }, {
                type: 'button',
                text: 'CORE.MODULE.MDM.USERS.MODAL.CHOOSE',
                icon: 'copy',
                onClick: function () {
                    _this.choseAuthorizePeople();
                }
            }]
        }
    },

    choseAuthorizePeople: function () {
        let _this = this;
        let grid = Gikam.getComp('lims-knowledgelibrary-edit-list-grid');
        let selections = grid.getSelections();
        if (Gikam.isEmpty(selections)) {
            Gikam.alert('GIKAM.TIP.CHOOSE_ONE_ITEM');
            return;
        }
        let userIds = '';
        if (selections.length === 1) {
            userIds = selections[0].authorizedPeopleIds;
        }
        Gikam.create('modal', {
            title: Gikam.choose.getConfig('common-user-choose').title,
            url: IFM_CONTEXT + Gikam.choose.getConfig('common-user-choose').url + Gikam.param({
                userIds: userIds
            }),
            onAfterClose: function (rows) {
                if (Gikam.isEmpty(rows)) {
                    return;
                }
                let authorizedPeople = '';
                let authorizedPeopleIds = '';
                rows.forEach(function (row) {
                    authorizedPeople = authorizedPeople + row.userName + ',';
                    authorizedPeopleIds = authorizedPeopleIds + row.id + ','
                });
                workspace.window.showMask();
                Gikam.postText(limsKnowledgelibrary.baseUrl + '/save-authorizedpeople', Gikam.getJsonWrapper({
                    authorizedPeople: authorizedPeople,
                    authorizedPeopleIds: authorizedPeopleIds
                }, [null, selections])).done(function () {
                    Gikam.alert('BASEDATA.MODULE.PRODUCT.CATEGORY.UPDATE.SUCCESS');
                    grid.refresh();
                }).always(function () {
                    workspace.window.closeMask();
                });
            }
        });
    },

    getRemarkNoteGridParam: function () {
        var _this = this;
        return {
            type: 'grid',
            id: 'lims-remark-note-edit-list-grid',
            url: limsKnowledgelibrary.baseRemarkNoteUrl + '/queries',
            service: 'limsRemarkNoteServiceImpl',
            dbTable: 'T_LIMS_REMARK_NOTE',
            columns: limsKnowledgelibrary.getRemarkNoteEditListColumns(),
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0);
                }
            },
            page: true,
            deleteFormatter: function (row) {
                return row.remarkNote.length < 20 ? row.remarkNote : row.remarkNote.substr(0, 8) + '...';
            },
            onRowActive: function (index, row) {
            },
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BUTTON.INSERT',
                icon: 'add',
                onClick: function () {
                    Gikam.preInsert({
                        modalTitle: 'GIKAM.BUTTON.INSERT',
                        fields: limsKnowledgelibrary.getRemarkNoteBaseInfoFormFields(),
                        url: limsKnowledgelibrary.baseRemarkNoteUrl,
                        height: 300
                    }).done(function (id) {
                        Gikam.getComp('lims-remark-note-edit-list-grid').refresh();
                    });
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.DELETE',
                icon: 'remove-row',
                onClick: function () {
                    Gikam.getComp('lims-remark-note-edit-list-grid').deleteRows(limsKnowledgelibrary.baseRemarkNoteUrl);
                }
            }]
        }
    },

    getExperimentInfoGridParam: function () {
        var _this = this;
        return {
            type: 'grid',
            id: 'lims-experiment-information-edit-list-grid',
            url: limsKnowledgelibrary.baseExperimentInfoUrl + '/queries',
            service: 'limsExperimentInformationServiceImpl',
            dbTable: 'T_LIMS_EXPERIMENT_INFORMATION',
            columns: limsKnowledgelibrary.getExperimentInfoEditListColumns(),
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0);
                }
            },
            onRowActive: function (index, row) {
            },
            deleteFormatter: function (row) {
                return row.title
            },
            page: true,
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BUTTON.INSERT',
                icon: 'add',
                onClick: function () {
                    Gikam.preInsert({
                        modalTitle: 'GIKAM.BUTTON.INSERT',
                        fields: limsKnowledgelibrary.getExperimentInfoBaseInfoFormFields(),
                        url: limsKnowledgelibrary.baseExperimentInfoUrl,
                        height: 300
                    }).done(function (id) {
                        Gikam.getComp('lims-experiment-information-edit-list-grid').refresh();
                    });
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.DELETE',
                icon: 'remove-row',
                onClick: function () {
                    Gikam.getComp('lims-experiment-information-edit-list-grid').deleteRows(limsKnowledgelibrary.baseExperimentInfoUrl);
                }
            }, {
                type: 'button',
                text: 'BASELINE.BASEDATA.EXPERIMENTINFOS.BUTTON.UPLOADPICTURE',
                icon: 'modify',
                onClick: function () {
                    var row = Gikam.getComp('lims-experiment-information-edit-list-grid').getActivedRow();
                    if (Gikam.isNotEmpty(row)) {
                        limsKnowledgelibrary.uploadPicture()
                    } else {
                        Gikam.alert('GIKAM.ALERT.CHOOSE.ONE.DATA');
                    }
                }
            }]
        }
    },

    getMainTab: function () {
        var _this = this;
        var first = false;
        return {
            type: 'tab',
            id: 'main-tab',
            panels: [{
                title: 'T_LIMS_PAGE_CONTENT_PAGE.LOGINPAGE',
                items: [{
                    type: 'form',
                    id: _this.idManager.loginPageForm,
                    service: 'limsPageContentConfigServiceImpl',
                    url: limsPageContentConfig.pageConfigUrl + '/1',
                    fields: limsPageContentConfig.getLoginPageFields(),
                    onLoadSuccess: function (data) {
                        Gikam.post(IFM_CONTEXT + '/core/module/item/files/queries', Gikam.getJsonWrapper({
                            f: {
                                targetId: 'T_LIMS_PAGE_CONTENT_CONFIG$1',
                                bizCategory_SEQ: 'LOGOPIC'
                            },
                            n: -1,
                            s: 50
                        })).done(function (fileList) {
                            if (!Gikam.isEmpty(fileList.rows)) {
                                Gikam.getComp(_this.idManager.loginPageForm).setData({
                                    ext$: {
                                        logopic: fileList.rows[0].downloadUrl
                                    }
                                }, false)
                            }
                        });
                    }
                }]
            }, {
                title: 'T_LIMS_PAGE_CONTENT_PAGE.INDEXPAGE',
                items: [{
                    type: 'form',
                    id: _this.idManager.indexPageForm,
                    service: 'limsPageContentConfigServiceImpl',
                    url: limsPageContentConfig.pageConfigUrl + '/2',
                    fields: limsPageContentConfig.getIndexPageFields(),
                    onLoadSuccess: function (data) {
                        Gikam.post(IFM_CONTEXT + '/core/module/item/files/queries', Gikam.getJsonWrapper({
                            f: {
                                targetId: 'T_LIMS_PAGE_CONTENT_CONFIG$2',
                                // bizCategory_SEQ : 'LOGOPIC'
                            },
                            n: -1,
                            s: 50
                        })).done(function (fileList) {
                            if (!Gikam.isEmpty(fileList.rows)) {
                                let logo = fileList.rows.filter(f => {
                                    return f.bizCategory === 'LOGOPIC'
                                })
                                let logomain = fileList.rows.filter(f => {
                                    return f.bizCategory === 'LOGOPIC_MAIN'
                                })
                                Gikam.getComp(_this.idManager.indexPageForm).setData({
                                    ext$: {
                                        logopic: logo[0].downloadUrl,
                                        logopicmain: logomain[0].downloadUrl,
                                    }
                                }, false)
                            }
                        });
                    }
                }]
            }, {
                title: 'BASELINE.BASEDATA.KNOWLEDGELIBRARYS.TAB.KNOWLEDGELIBRARYS',
                id: 'tab-1',
                items: [_this.getActivedGridParam()]
            }, {
                title: 'BASELINE.BASEDATA.KNOWLEDGELIBRARYS.TAB.REMARKNOTES',
                id: 'tab-2',
                items: [_this.getRemarkNoteGridParam()]
            }, {
                title: 'BASELINE.BASEDATA.KNOWLEDGELIBRARYS.TAB.EXPERIMENTINFO',
                id: 'tab-3',
                items: [_this.getExperimentInfoGridParam()]
            }],
            onActived: function (data) {
                var tab = Gikam.getComp('main-tab');
                switch (data.index) {
                    case 2:
                        if (first) {  // 首次加载页面时，不操作
                            Gikam.getAsyncComp('lims-knowledgelibrary-edit-list-grid').done(function (grid) {
                                grid.refresh()
                            })
                        }
                        break;
                    case 3:
                        Gikam.getAsyncComp('lims-remark-note-edit-list-grid').done(function (grid) {
                            grid.refresh()
                        });
                        break;
                    case 4:
                        Gikam.getAsyncComp('lims-experiment-information-edit-list-grid').done(function (grid) {
                            grid.refresh()
                        });
                        break;
                }
                first = true;
            }
        }
    },

    idManager: {
        loginPageForm: 'lims-logpage-config-form',
        indexPageForm: 'lims-indexpage-config-form'
    },

    uploadLogoPic: function (idStr, bizId) {
        var configForm = Gikam.getComp(idStr);
        Gikam.post(IFM_CONTEXT + '/core/module/item/files/queries', Gikam.getJsonWrapper({
            f: {
                targetId: 'T_LIMS_PAGE_CONTENT_CONFIG$' + bizId,
                bizCategory_SEQ: 'LOGOPIC'
            },
            n: -1,
            s: 50
        })).done(function (r) {
            if (r.total > 0) {
                Gikam.confirm('GIKAM.BUTTON.CONFIRM', 'LIMS.PAGE.CONFIG.LOGO.OVERRIDE', function () {
                    Gikam.create('simpleUploader', {
                        id: 'page-config-logo-pic-uploader',
                        dbTable: 'T_LIMS_PAGE_CONTENT_CONFIG',
                        bizId: bizId,
                        bizCategory: 'LOGOPIC',
                        accept: 'image',
                        multiple: false,
                        onAfterClose: function (fileList) {
                            if (fileList.length > 0) {
                                Gikam.del(IFM_CONTEXT + '/core/module/item/files', Gikam.getJsonWrapper(null, ['', r.rows]));
                                Gikam.getJson(Gikam.printf(IFM_CONTEXT + '/core/module/item/files/{id}', {
                                    id: fileList[0].id
                                })).done(function (r) {
                                    configForm.setData({
                                        ext$: {
                                            logopic: r.downloadUrl
                                        }
                                    }, false)
                                });
                            }
                        }
                    });
                })
            } else {
                Gikam.create('simpleUploader', {
                    id: 'page-config-logo-pic-uploader',
                    dbTable: 'T_LIMS_PAGE_CONTENT_CONFIG',
                    bizId: bizId,
                    bizCategory: 'LOGOPIC',
                    accept: 'image',
                    multiple: false,
                    onAfterClose: function (fileList) {
                        if (fileList.length === 0) {
                            return;
                        }
                        Gikam.getJson(Gikam.printf(IFM_CONTEXT + '/core/module/item/files/{id}', {
                            id: fileList[0].id
                        })).done(function (r) {
                            configForm.setData({
                                ext$: {
                                    logopic: r.downloadUrl
                                }
                            }, false)
                        });
                    }
                });
            }
        });
    },

    create: function () {
        var _this = this;
        Gikam.create('layout', {
            id: 'lims-knowledgelibrary-edit-list-layout',
            renderTo: workspace.window.$dom,
            center: {
                items: [this.getMainTab()]
            }
        });
    },

    init: function () {
        this.create();
    }
};
